package action;

import bean.Sell;
import dao.SellDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SellServlet")
public class SellServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String method = request.getParameter("method1");
        if (method == null || method.equals("query")) {
            List<Sell> vip = SellDao.selvip();
            request.setAttribute("list11", vip);
            request.getRequestDispatcher("xs.jsp").forward(request, response);
        } else if (method.equals("delete")) {
            String id = request.getParameter("id");
            SellDao.del(id);
            response.sendRedirect("SellServlet");
        } else if (method.equals("huanhuan")) {
            int id = Integer.parseInt(request.getParameter("id"));
            Sell shop = SellDao.upda(id);
            System.out.println(shop);
            request.setAttribute("list", shop);
            request.getRequestDispatcher("updatesell.jsp").forward(request,
                    response);
        } else if (method.equals("upd")) {
            System.out.println("修改");
            Sell shop = new Sell();
            SellDao dao = new SellDao();

            int id = Integer.parseInt(request.getParameter("onum"));
            System.out.println(id);
            String name = request.getParameter("name");
            int address = Integer.parseInt(request.getParameter("gender"));
            int time = Integer.parseInt(request.getParameter("age"));
            shop.setOnum(id);
            shop.setName(name);
            shop.setGender(address);
            shop.setAge(time);
            // String name2=new String(name.getBytes("ISO-8859-1"),"utf-8");
            System.out.println(shop);
            dao.upda1(shop);
            response.sendRedirect("SellServlet");

        }
 else if (method.equals("insert")) {
            System.out.println("增加");
            response.setCharacterEncoding("utf-8");
            int no = Integer.parseInt(request.getParameter("onum"));
            String name = request.getParameter("name");
            int address = Integer.parseInt(request.getParameter("gender"));
            int time = Integer.parseInt(request.getParameter("age"));
            System.out.println(no);
            SellDao.insert(no, name, time, address);
            response.sendRedirect("SellServlet");

            // }else if (method.equals("test")) {
            // response.setCharacterEncoding("utf-8");
            // PrintWriter pw=response.getWriter();
            //
            // pw.write("jintisajf");
            // pw.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
    }
}
